/**
 * 
 */
package com.yerba.ricecooker.datasource;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.StringTokenizer;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;

import com.yerba.ricecooker.quote.Dividend;
import com.yerba.ricecooker.quote.Quote;
import com.yerba.ricecooker.utility.CommonHelper;
import com.yerba.ricecooker.utility.CommonLogger;

/**
 * @author mxie
 * 
 */
public class YahooLoader extends BaseLoader
{
    CommonLogger logger = CommonLogger.instance();

    public void load(String symbol)
    {
        getQuotes().clear();
        getQuotes().setSymbol(symbol);

        CommonHelper.disableApacheLogging();
        GetMethod get = null;

        try
        {
            HttpClient client = null;

            Calendar today = Calendar.getInstance();
            String today_year = String.valueOf(today.get(Calendar.YEAR));
            String today_month = String.valueOf(today.get(Calendar.MONTH));
            String today_day = String.valueOf(today.get(Calendar.DAY_OF_MONTH));

            symbol = URLEncoder.encode(symbol, "UTF-8");
            String url = "http://ichart.finance.yahoo.com/table.csv?s=" + symbol + "&d=" + today_day + "&e=" + today_month
                    + "&f=" + today_year + "&g=d&a=0&b=1&c=1920&ignore=.csv";

            String strLine = null;
            StringTokenizer st = null;

            System.getProperties().setProperty("httpclient.useragent", "Mozilla/4.0");
            client = new HttpClient(new MultiThreadedHttpConnectionManager());

            get = new GetMethod(url);
            get.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");

            get.setFollowRedirects(true);
            client.executeMethod(get);
            InputStream in = get.getResponseBodyAsStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String previousLine = "--Yahoo-CSV-Start--";
            br.readLine();

            while ((strLine = br.readLine()) != null)
            {
                previousLine = strLine;

                Quote quote = new Quote();
                st = new StringTokenizer(strLine, ",");

                // date
                if (st.hasMoreTokens())
                    quote.setDate(st.nextToken().trim());
                else
                    throw new Exception(previousLine);
                // open
                if (st.hasMoreTokens())
                    quote.setOpen(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);
                // high
                if (st.hasMoreTokens())
                    quote.setHigh(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);
                // low
                if (st.hasMoreTokens())
                    quote.setLow(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);
                // close
                if (st.hasMoreTokens())
                    quote.setClose(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);
                quote.setTrueClose(quote.getClose());
                // volume
                if (st.hasMoreTokens())
                    quote.setVolume(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);
                // adjclose
                if (st.hasMoreTokens())
                    quote.setAdjclose(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);

                getQuotes().add(quote);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            getQuotes().clear();
        }
        finally
        {
            get.releaseConnection();
        }

        if (getQuotes().size() < 1) return;

        // get dividend data
        try
        {
            HttpClient client = null;

            Calendar today = Calendar.getInstance();
            String today_year = String.valueOf(today.get(Calendar.YEAR));
            String today_month = String.valueOf(today.get(Calendar.MONTH));
            String today_day = String.valueOf(today.get(Calendar.DAY_OF_MONTH));

            symbol = URLEncoder.encode(symbol, "UTF-8");
            String url = "http://ichart.finance.yahoo.com/table.csv?s=" + symbol + "&d=" + today_day + "&e=" + today_month
                    + "&f=" + today_year + "&g=v&a=0&b=1&c=1920&ignore=.csv";

            String strLine = null;
            StringTokenizer st = null;

            System.getProperties().setProperty("httpclient.useragent", "Mozilla/4.0");
            client = new HttpClient(new MultiThreadedHttpConnectionManager());

            get = new GetMethod(url);
            get.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");

            get.setFollowRedirects(true);
            client.executeMethod(get);
            InputStream in = get.getResponseBodyAsStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String previousLine = "--Yahoo-CSV-DIV-Start--";
            br.readLine();

            while ((strLine = br.readLine()) != null)
            {
                if (strLine.toLowerCase().contains("404 not found"))
                    break;
                
                Dividend div = new Dividend();
                st = new StringTokenizer(strLine, ",");

                // date
                if (st.hasMoreTokens())
                    div.setDate(st.nextToken().trim());
                else
                    throw new Exception(previousLine);
                // dividend
                if (st.hasMoreTokens())
                    div.setDividend(Double.valueOf(st.nextToken().trim()).doubleValue());
                else
                    throw new Exception(previousLine);

                getQuotes().getDividends().insert(div.getDate(), div);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            get.releaseConnection();
        }

        // calculate true close values
        getQuotes().calculateTrueClose();
    }

}
